const {v4: uuidv4} = require('uuid')
const Tags = require('../../models/tags')

const getTagsHandler = async (_, res) => {
  try {
    const tags = await Tags.find()
    res.status(200).json(tags)
  } catch (e) {
    res.status(500).json({success: false, msg: e})
  }
}

const postNewTagHandler = async (req, res) => {
  const {text, color, userId, projectId} = req.body

  try {
    const tag = await Tags.create({id: uuidv4(), text, color, userId, projectId})
    res.status(200).json(tag)
  } catch (e) {
    res.status(500).json({success: false, msg: e})
  }
}

module.exports = {getTagsHandler, postNewTagHandler}
